package com.jack.atomic;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * @Author jack chan
 * @Description //测试Atomic和普通类型在多线程下区别
 * @Date 12:14 上午 2020/8/9
 **/
public class TestAtomic {
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    private static Integer integer = 0;

    public static void main(String[] args) {
        CountDownLatch countDownLatch = new CountDownLatch(1);
        for (int i = 0; i < 10; i++) {
            new Thread(new myRunnable(countDownLatch)).start();
        }

        countDownLatch.countDown();
    }

    static class myRunnable implements Runnable{
        CountDownLatch latch;


        public myRunnable(CountDownLatch latch){
            this.latch = latch;
        }

        @Override
        public  void run() {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(TestAtomic.class) {
                latch.countDown();
                integer++;
                System.out.println(integer);
                System.err.println(atomicInteger.incrementAndGet());
            }

        }
    }

}
